home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1974 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: gate.net!pslfl2-12
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
  4. Subject: Re: Stupid array problems
  5. Date: 14 Jan 1996 17:44:01 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4dbfd1$2hpc@news.gate.net>
  8. References: <4d9b9v$14n@paperboy.ids.net>
  9. NNTP-Posting-Host: pslfl2-12.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <4d9b9v$14n@paperboy.ids.net>,
  13.    scarney@conan.ids.net (scarney) wrote:
  14. >Ok, I've been having what I thought would be a simple problem but no one 
  15. >seems to quite figure out. I Have an array of strings, both the key and 
  16. >the string being pointers. Putting information onto the array is like a 
  17. >stack.  My problem comes in the removal of information from it. Popping 
  18. >from a stack only pulls the lastelement out of the array, I want to pull 
  19. >any element out. Destroying the data isn't the hard part because I just 
  20. >blow up the pointers...the problem is that the indexing for the array 
  21. >gets kind of screwy. If I have an array organized by integers with 
  22. >elements 1 2 3 4 5 6 7 8, if I decided to kill element #4 I'm going to 
  23. >have an array of 1 2 3 5 6 7 8, and as time goes on its going to get more 
  24. >screwy. Is there an easy way to delete an element while preserving some 
  25. >semblance of sequence in the array indexing? In Perl I'd use the splice() 
  26. >command...is there anything similar in C/C++?
  27. >
  28. >I get the feeling I'm looking at this from a completely wrong 
  29. >perspective, so don't flame me if I am...
  30. >
  31. >Thanks.
  32. >-Seth
  33. >
  34.  
  35. If you want to maintain the same order in the array you only need to move the 
  36. pointers. Assuming you indicate the end with a NULL pointer and you need to 
  37. free the string and *i* indexes the pointer to the string to remove it might 
  38. go something like this:
  39.  
  40.     free(array[i]);
  41.     for(;array[i];array[i]=array[i+1],i++);
  42.  
  43. This moves all pointers beyond the pointer you're removing *down one* 
  44. including the NULL.
  45.  
  46. Bill
  47.  
  48. "Whatcha got on?...Your mind?"
  49.